home *** CD-ROM | disk | FTP | other *** search
/ CDUTIL 13 / CDUTIL #13 Julio 1995.iso / windows / acadcom / ads / sample / tower.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-08  |  12.9 KB  |  510 lines

  1. /* Next available MSG number is  20 */
  2.  
  3. /*
  4.       TOWER.C
  5.  
  6.    Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994 by Autodesk, Inc.
  7.  
  8.    Permission to use, copy, modify, and distribute this software in 
  9.    object code form for any purpose and without fee is hereby granted, 
  10.    provided that the above copyright notice appears in all copies and 
  11.    that both that copyright notice and the limited warranty and 
  12.    restricted rights notice below appear in all supporting 
  13.    documentation.
  14.  
  15.    AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.  
  16.    AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF 
  17.    MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK, INC.
  18.    DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE 
  19.    UNINTERRUPTED OR ERROR FREE.
  20.  
  21.    Use, duplication, or disclosure by the U.S. Government is subject to 
  22.    restrictions set forth in FAR 52.227-19 (Commercial Computer 
  23.    Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) 
  24.    (Rights in Technical Data and Computer Software), as applicable.
  25.     
  26.    .
  27.  
  28.         Tower of Hanoi in C
  29.  
  30.         Implemented by John Lynch   December 1988
  31.  
  32.         This is basically a translation of tower.lsp written by
  33.         Kelvin R. Throop.
  34.  
  35.        This file implements the Tower of Hanoi problem.  It is as
  36.        specified in the rules:
  37.  
  38.           1.  Only one disc may be moved at a time.
  39.           2.  No disc may be placed on top of a smaller one.
  40.  
  41.        The only incompatibility with the original is that the universe
  42.        will not come to an end when this function completes (however, if
  43.        you run it with the specified number of discs, 64, the protons may
  44.        decay before it's done).
  45.  
  46.        One defines the tower with the command TOWER, which asks for the
  47.        number of discs.  Scaling is automatic, as is clearing away of any
  48.        previous execution.  Once the tower is defined, the solution may
  49.        be accomplished with the command SOLVE.
  50.  
  51.        The solution function, TRANSFER, is as given in Winston and Horn,
  52.        "LISP", second edition, pp. 112-114.
  53.  
  54.  
  55. */
  56.  
  57. #include  <stdio.h>
  58. #include  <string.h>
  59. #include  "adslib.h"
  60.  
  61.  
  62. /* Local functions */
  63. static int loadfuncs _((void));
  64. static int dofun _((void));
  65. static int tower2 _((void));
  66. static int solve2 _((void));
  67. #ifdef __ZTC__
  68. static int movedisc _((int from, int to));
  69. static int transfer _((int from, int to, int spare, int n));
  70. #else
  71. static int movedisc _((short from, short to));
  72. static int transfer _((short from, short to, short spare, short n));
  73. #endif
  74.  
  75.  
  76. /* For convenience, make the global variables the same as in the LISP
  77.    version */
  78.  
  79. int nrings = 0,                       /* Number of rings */
  80.     before = 0,                       /* Whether or not we have run before */
  81.     armed = 0;                        /* Set if we are ready to solve */
  82.  
  83. ads_real bthick = 1.0,                /* Base thickness */
  84.          rthick = 1.0,                /* Ring thickness */
  85.          smring = 1.5,                /* Smallest ring */
  86.          ringinc = 1.0,               /* Ring size increment */
  87.          postdia = 1.0,               /* Post diameter */
  88.          airspace = 0.1,              /* Airspace */
  89.          rspace = 1.1,                /* Total ring space */
  90.          postposx[3],
  91.          postposy;
  92.  
  93.  
  94. /* Some of the common strings used to call AutoCAD's Command processor */
  95.  
  96. char layer[] = /*MSG0*/"_.layer",
  97.      vpoint[] = /*MSG0*/"_.vpoint",
  98.      erase[] = /*MSG0*/"_.erase",
  99.      new[] = /*MSG0*/"_new",
  100.      set[] = /*MSG0*/"_set",
  101.      base[] = /*MSG6*/"base",
  102.      basetc[] = /*MSG7*/"base,1,2,3,4,5,6",
  103.      color[] = /*MSG0*/"_color",
  104.      elev[] = /*MSG0*/"_.elev",
  105.      solid[] = /*MSG0*/"_.solid",
  106.      zoom[] = /*MSG0*/"_.zoom",
  107.      circle[] = /*MSG0*/"_.circle",
  108.      origin[] = "0,0,0",
  109.      oneoneone[] = "1,1,1",
  110.      d[] = /*MSG0*/"_d",
  111.      l[] = /*MSG0*/"_l",
  112.      e[] = /*MSG0*/"_e",
  113.      nullstring[] = "";
  114.  
  115.  
  116. /* The postlist structure is used to hold the disks */
  117. struct disk {
  118.      struct disk *next;
  119.      short layer;
  120.      ads_real r;
  121. };
  122.  
  123. struct postlist {
  124.      struct disk *top;
  125.      short count;
  126. };
  127.  
  128. struct disk *disks;
  129. struct postlist postl[3];
  130.  
  131.  
  132. /* MAIN -- the main routine */
  133. void
  134. main(argc,argv)
  135.   int argc; char *argv[];
  136. {
  137.     int stat;
  138.     short scode = RSRSLT;
  139.     char errmsg[80];
  140.  
  141.     ads_init(argc, argv);
  142.  
  143.     for ( ;; ) {
  144.  
  145.         if ((stat = ads_link(scode)) < 0) {
  146.             sprintf(errmsg,
  147.                     /*MSG16*/"TOWER: bad status from ads_link() = %d\n",
  148.                     stat);
  149. #ifdef Macintosh
  150.             macalert(errmsg);
  151. #else
  152.             puts(errmsg);
  153.             fflush(stdout);
  154. #endif /* Macintosh */
  155.             exit(1);
  156.         }
  157.  
  158.         scode = RSRSLT;               /* default return code */
  159.  
  160.         /* process the AutoLisp request */
  161.         switch (stat) {
  162.  
  163.         case RQXLOAD:                 /* register our function names */
  164.             scode = loadfuncs() ? -RSRSLT : -RSERR;
  165.             break;
  166.  
  167.         case RQXUNLD:                 /* unload our functions */
  168.             break;
  169.  
  170.         case RQSUBR:                  /* execute registered function */
  171.             dofun();
  172.             break;
  173.  
  174.         default:
  175.             break;
  176.         }
  177.     }
  178. }
  179.  
  180.  
  181.  
  182. /* LOADFUNCS  --  Register (or define) external functions with AutoLISP */
  183. static int
  184. loadfuncs()
  185. {
  186.     if (!ads_defun(/*MSG0*/"C:TOWER2", 0))        /* tower2 command has id 0 */
  187.         return 0;
  188.     if (!ads_defun(/*MSG0*/"C:SOLVE2", 1))        /* solve2 command has id 1 */
  189.         return 0;
  190.  
  191.     ads_printf(/*MSG17*/"\
  192. Use tower2 to initialize, and solve2 to solve the tower\n");
  193.  
  194.     return 1;
  195. }
  196.  
  197. /* Execute registered functions here */
  198. static int
  199. dofun()
  200. {
  201.     int id;
  202.  
  203.     /* Get the function id */
  204.     if ((id = ads_getfuncode()) < 0)
  205.         return 0;
  206.  
  207.     /* No arguments are passed as a part of C:XXX functions */
  208.  
  209.  
  210.     switch (id) {                     /* Which function is being called */
  211.     case 0:
  212.         if (!tower2())
  213.             return 0;
  214.         break;
  215.  
  216.     case 1:
  217.         if (!solve2())
  218.             return 0;
  219.         break;
  220.     }
  221.     return 1;
  222. }
  223.  
  224. /* Handle setup of tower */
  225.  
  226. static int
  227. tower2()
  228. {
  229.     ads_real lring;                   /* largest ring diameter */
  230.     int a, i;
  231.     ads_real bwidth, blength;         /* width and length of base */
  232.     ads_real x, y, z, r;
  233.     struct resbuf genrb;
  234.     ads_point pt1, pt2, pt3, pt4;
  235.  
  236.     bthick = rthick = ringinc = postdia = 1.0;
  237.     smring = 1.5;
  238.     airspace = 0.1;
  239.     rspace = 1.1;
  240.  
  241.  
  242.     /* Disallow null, zero and negative responses.  */
  243.     ads_initget(7, NULL);
  244.     ads_getint(/*MSG18*/"Enter number of rings: ", &nrings);
  245.  
  246.     lring = smring + (nrings * ringinc);
  247.  
  248.     /* reset from possible previous run */
  249.  
  250.     postl[0].top   = postl[1].top   = postl[2].top = NULL;
  251.     postl[0].count = postl[1].count = postl[2].count = 0;
  252.  
  253.  
  254.     disks = (struct disk *) malloc ((nrings + 1) * (sizeof (struct disk)));
  255.  
  256.     rspace = rthick + airspace;       /* Actual ring spacing */
  257.  
  258.     /* set up the appropriate environment variables */
  259.     genrb.restype = RTSHORT;
  260.     genrb.resval.rint = 0;
  261.     ads_setvar(/*MSG0*/"blipmode", &genrb);
  262.     ads_setvar(/*MSG0*/"cmdecho", &genrb);
  263.     ads_setvar(/*MSG0*/"fillmode", &genrb);
  264.  
  265.  
  266.     if (before) {
  267.  
  268.         ads_command(RTSTR, vpoint, RTSTR, origin, NULL);
  269.  
  270.         a = 1;
  271.  
  272.         while (a <= before) {
  273.  
  274.             ads_command(RTSTR, erase, RTSTR, l, RTSTR, nullstring, NULL);
  275.  
  276.             a++;
  277.  
  278.         }
  279.  
  280.         ads_command(RTSTR, layer, RTSTR, set, RTSTR, base, RTSTR,
  281.                     nullstring, NULL);
  282.  
  283.     } else {
  284.  
  285.         /* Note:  this leave the command function "open" (non-terminated) */
  286.         if (!ads_tblsearch (/*MSG0*/"layer", base, 1))
  287.             ads_command(RTSTR, layer, RTSTR, new, RTSTR, basetc, RTSTR, color,
  288.                         RTSHORT, 7, RTSTR, base, NULL);
  289.  
  290.  
  291.         a = 0;
  292.  
  293.         while (++a <= 6) {
  294.  
  295.             ads_command(RTSTR, color, RTSHORT, a, RTSHORT, a, NULL);
  296.         }
  297.  
  298.         /* Set the current layer (command still "open") */
  299.  
  300.  
  301.         ads_command(RTSTR, set, RTSTR, base, RTSTR, nullstring, NULL);
  302.  
  303.     }
  304.  
  305.     /* Draw the base */
  306.  
  307.     /* Command: "elev" 0.0 bthick */
  308.  
  309.     ads_command(RTSTR, elev, RTREAL, 0.0, RTREAL, bthick, NULL);
  310.  
  311.  
  312.     bwidth = lring + 2 * postdia;
  313.     blength = 3 * (postdia + lring) + postdia;
  314.  
  315.     ads_command(RTSTR, vpoint, RTSTR, origin, NULL);
  316.  
  317.     pt1[X] = pt1[Y] = 0.0;
  318.     pt2[Y] = pt4[Y] = bwidth;
  319.     pt3[X] = pt4[X] = blength;
  320.     pt2[X] = pt3[Y] = 0.0;
  321.  
  322.     ads_command(RTSTR, solid, RTPOINT, pt1, RTPOINT, pt2, RTPOINT, pt3,
  323.                 RTPOINT, pt4, NULL);
  324.  
  325.  
  326.     /* We must use ads_command(0) for ads_command(NULL) to be compatible */
  327.     ads_command(0);
  328.  
  329.     ads_command(RTSTR, zoom, RTSTR, e, NULL);
  330.  
  331.     /* Draw the posts */
  332.  
  333.     /* Set the elevation through the command function */
  334.     ads_command(RTSTR, elev, RTREAL, bthick, RTREAL,
  335.                 (nrings + 1) * rspace, NULL);
  336.  
  337.     x = postdia + lring;
  338.     y = lring / 2 + postdia;
  339.  
  340.     postposx[0] = y;
  341.     postposx[1] = y + x;
  342.     postposx[2] = y + x + x;
  343.  
  344.     postposy = postdia + lring / 2;
  345.  
  346.     pt1[Y] = postposy;
  347.  
  348.     for (i = 0; i < 3; i++) {
  349.         pt1[X] = postposx[i];
  350.         ads_command(RTSTR, circle, RTPOINT, pt1, RTSTR, d, RTREAL,
  351.                     postdia, NULL);
  352.     }
  353.  
  354.     bthick += airspace;               /* Offset position of lowest ring */
  355.  
  356.     ads_command(RTSTR, vpoint, RTSTR, oneoneone, NULL);
  357.  
  358.  
  359.     /* Draw the rings, placing them on post 1 initially */
  360.  
  361.     pt1[X] = y;
  362.     pt1[Y] = postposy;
  363.  
  364.     a = 6;
  365.     z = bthick;
  366.     r = lring / 2;
  367.     for (i = 1; i <= nrings; i++) {
  368.  
  369.         /* Command: "layer" "set" (l%6 + 1) ""  */
  370.         ads_command(RTSTR, layer, RTSTR, set, RTSHORT, a%6 + 1,
  371.                     RTSTR, nullstring, NULL);
  372.  
  373.         /* Command:  "elev" z rthick  */
  374.  
  375.         ads_command(RTSTR, elev, RTREAL, z, RTREAL, rthick, NULL);
  376.  
  377.         /* Command: "circle" (m postposy) r  */
  378.  
  379.         pt1[Z] = z;
  380.         ads_command(RTSTR, circle, RTPOINT, pt1, RTREAL, r, NULL);
  381.  
  382.  
  383.         /* Hang it on the postlist */
  384.         disks[i].layer = a%6 + 1;
  385.         disks[i].r = r;
  386.         disks[i].next = postl[0].top;
  387.  
  388.  
  389.         postl[0].top = &disks[i];
  390.         postl[0].count++;
  391.  
  392.         r -= ringinc / 2;
  393.         z += rspace;
  394.         a++;
  395.  
  396.  
  397.     }
  398.  
  399.     before = nrings + 4;
  400.  
  401.     genrb.restype = RTSHORT;
  402.     genrb.resval.rint = 0;
  403.     ads_setvar(/*MSG0*/"cmdecho", &genrb);
  404.  
  405.     armed = TRUE;
  406.     ads_retvoid();
  407.  
  408.     return TRUE;
  409.  
  410. }
  411.  
  412. /* Solve the tower problem. */
  413.  
  414. static int
  415. solve2()
  416. {
  417.     if (!armed) {
  418.         ads_printf(/*MSG19*/"You must tower2 before solving\n");
  419.         return TRUE;
  420.     } else
  421.         armed = FALSE;
  422.  
  423.     transfer(1, 2, 3, nrings);
  424.     ads_redraw(NULL, 0);
  425.     ads_retvoid();
  426.     return  TRUE;
  427. }
  428.  
  429. #ifdef __STDC__
  430. static int movedisc(short from, short to)
  431. #else
  432. static int
  433. movedisc(from, to)
  434.   short from, to;
  435. #endif
  436. {
  437.     struct disk *lfrom, *lto;
  438.     ads_point pt;
  439.  
  440.     /* Get the current heads of the to and from lists */
  441.     lfrom = postl[from - 1].top;
  442.     lto = postl[to - 1].top;
  443.  
  444.     /* Remove this disk from the head of the from list */
  445.     postl[from - 1].top = lfrom->next;
  446.     postl[from - 1].count--;
  447.  
  448.     /* Command: "layer" "set" lfrom->layer "" */
  449.  
  450.     ads_command(RTSTR, layer, RTSTR, set, RTSHORT, lfrom->layer,
  451.                 RTSTR, nullstring, NULL);
  452.  
  453.     /* Command: "elev" b rthick */
  454.  
  455.     ads_command(RTSTR, elev, RTREAL, bthick + rspace * postl[from - 1].count,
  456.                 RTREAL, rthick, NULL);
  457.  
  458.  
  459.     /* Command: "erase" postposx[from - 1], postposy+lfrom->r ""  */
  460.  
  461.     pt[X] = postposx[from - 1];
  462.     pt[Y] = postposy + lfrom->r;
  463.     pt[Z] = bthick + rspace * postl[from-1].count;
  464.  
  465.     ads_command(RTSTR, erase, RTPOINT, pt, RTSTR, nullstring, NULL);
  466.  
  467.     /* Command: "elev" (bthick + postl[to-1]->count*rspace  rthick */
  468.  
  469.     ads_command(RTSTR, elev, RTREAL, bthick + postl[to-1].count * rspace,
  470.                 RTREAL, rthick, NULL);
  471.  
  472.  
  473.     /* Command: "circle" postpostx[to - 1],postposy lfrom->r */
  474.  
  475.     pt[X] = postposx[to - 1];
  476.     pt[Y] = postposy;
  477.     pt[Z] = bthick + postl[to-1].count * rspace;
  478.  
  479.     ads_command(RTSTR, circle, RTPOINT, pt, RTREAL, lfrom->r, NULL);
  480.  
  481.  
  482.     /* Place the disk on the top of the to list */
  483.     lfrom->next = lto;
  484.     postl[to - 1].top = lfrom;
  485.     postl[to - 1].count++;
  486.  
  487.     return TRUE;
  488. }
  489.  
  490.  
  491. #ifdef __STDC__
  492. static int transfer(short from, short to, short spare, short n)
  493. #else
  494. static int
  495. transfer(from, to, spare, n)
  496.   short from, to, spare, n;
  497. #endif
  498. {
  499.     if (n == 0)
  500.         return TRUE;
  501.     else if (n == 1)
  502.         movedisc(from, to);
  503.     else {
  504.         transfer(from, spare, to, n - 1);
  505.         movedisc(from, to);
  506.         transfer(spare, to, from, n - 1);
  507.     }
  508.     return TRUE;
  509. }
  510.